package com.xiaozhou.controller;

import com.xiaozhou.function.FunctionManager;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.service.v4.embedding.EmbeddingApiResponse;
import com.zhipu.oapi.service.v4.embedding.EmbeddingRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shhzhou
 * @description: 向量模型 例子
 * @date 2024/11/4 14:34
 */
@RestController
@RequestMapping("/embedding")
public class EmbeddingController {

    @Autowired
    private ClientV4 clientV4;

    @Autowired
    private FunctionManager functionManager;


    /**
     * @return
     * @return com.zhipu.oapi.service.v4.embedding.EmbeddingApiResponse
     * @Author xiaozhou
     * @Description  embedding2 文本转向量化 例子  （向量的检索会优于文本检索，这里需要配合向量数据库进行保存使用，主流的向量数据库Elasticsearch等，某些小场景下可以用redis、mongodb简单替代）
     * @Date 14:42 2024/11/4
     * @Param message
     */
    @GetMapping("/embedding2Model")
    public EmbeddingApiResponse embedding2Model(String message) {
        EmbeddingRequest request = new EmbeddingRequest();
        request.setModel("embedding-2");
        request.setInput(message);

        EmbeddingApiResponse embeddingApiResponse = clientV4.invokeEmbeddingsApi(request);

        return embeddingApiResponse;
    }


    @GetMapping("/embedding3Model")
    public EmbeddingApiResponse embedding3Model(String message) {
        EmbeddingRequest request = new EmbeddingRequest();
        request.setModel("embedding-3");
        request.setInput(message);
        // 自定义向量维度参数，非必选，不填也可以，默认是2048
        request.setDimensions(256);

        EmbeddingApiResponse embeddingApiResponse = clientV4.invokeEmbeddingsApi(request);

        return embeddingApiResponse;
    }


}
